Dane - Credit Cards Customers.
Problem - zbadaj którzy klienci są chętni do zrezygnowania z usługi.
Do zadania został użyty model Xgboost.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import seaborn as sns
plt.style.use('ggplot')
import dalex as dx
import pickle
import xgboost as xgb
from sklearn.model_selection import train_test_split
np.random.seed(23)
input_df = pd.read_csv('data/preprocessed_dataset.csv')
input_df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 10127 entries, 0 to 10126 Data columns (total 25 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Gender 10127 non-null int64 1 Missing_Income 10127 non-null int64 2 Income_Category 10127 non-null int64 3 Missing_Education 10127 non-null int64 4 Education_Level 10127 non-null int64 5 Marital_Divorced 10127 non-null int64 6 Marital_Married 10127 non-null int64 7 Marital_Single 10127 non-null int64 8 Marital_Unknown 10127 non-null int64 9 Card_Blue 10127 non-null int64 10 Card_Gold 10127 non-null int64 11 Card_Platinum 10127 non-null int64 12 Card_Silver 10127 non-null int64 13 Months_on_book 10127 non-null int64 14 Avg_Utilization_Ratio 10127 non-null float64 15 Avg_Open_To_Buy 10127 non-null float64 16 Total_Trans_Amt 10127 non-null int64 17 Dependent_count 10127 non-null int64 18 Total_Relationship_Count 10127 non-null int64 19 Months_Inactive_12_mon 10127 non-null int64 20 Contacts_Count_12_mon 10127 non-null int64 21 Total_Revolving_Bal 10127 non-null int64 22 Total_Amt_Chng_Q4_Q1 10127 non-null float64 23 Total_Ct_Chng_Q4_Q1 10127 non-null float64 24 Attrition 10127 non-null int64 dtypes: float64(4), int64(21) memory usage: 1.9 MB
y = input_df.loc[:,'Attrition']
X = input_df.drop('Attrition', axis='columns')
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=14)
xgboost = pickle.load(open("xgb_model.p", "rb" ))
explainer = dx.Explainer(xgboost, X_train, y_train, label = "Random Forest")
Preparation of a new explainer is initiated -> data : 7595 rows 24 cols -> target variable : Parameter 'y' was a pandas.Series. Converted to a numpy.ndarray. -> target variable : 7595 values -> model_class : xgboost.sklearn.XGBClassifier (default) -> label : Random Forest -> predict function : <function yhat_proba_default at 0x000001B450578790> will be used (default) -> predict function : Accepts pandas.DataFrame and numpy.ndarray. -> predicted values : min = 9.37e-06, mean = 0.841, max = 1.0 -> model type : classification will be used (default) -> residual function : difference between y and yhat (default) -> residuals : min = -0.779, mean = -5.26e-05, max = 0.7 -> model_info : package xgboost A new explainer has been created!
Attrition:
1-existing
0-attrited
explainer.predict(X_train)[0]
0.9727738
cp = explainer.predict_profile(X_train.iloc[0,:])
Calculating ceteris paribus: 100%|████████████████████████████████████████████████████| 24/24 [00:00<00:00, 163.36it/s]
cp.plot()
Predykcja dla obserwacji o indeksie 0 zmalałaby z 0.97 do 0.45 gdyby Months_on_book(liczba miesięcy od udzielenia pożyczki) wzrosło wzrosło do 44.
gdyby Total_Trans_Amt(liczba transakcji w ostatnich 12 miesiącach) spadło poniżej 1000 predykcja spdałaby do 0.18.
cp = explainer.predict_profile(X_train.iloc[57,:])
cp.plot()
Calculating ceteris paribus: 100%|████████████████████████████████████████████████████| 24/24 [00:00<00:00, 193.66it/s]
cp = explainer.predict_profile(X_train.iloc[61,:])
cp.plot()
Calculating ceteris paribus: 100%|████████████████████████████████████████████████████| 24/24 [00:00<00:00, 224.43it/s]
Porównujemy obserwację 57. i 61. dla których predykcje wynoszą 0.974 i 0.061. ZMienna Gender reprezentuje płeć użytkownika: 1-mężczyzna, 0-kobieta. Dla obserwacji 61. bycie kobietą zwiększa predykcję, a dla obserwacji 57. zmnijesza. Dla zmiennej Months on book. Dla obserwaji 57. predykcja rośnie wraz ze wzrtostem Months on book, z dwoma małymi spadkami dla wartości 15 oraz 30, Dla obserwacji 61. wzrost wartości zmiennej przyczynia się do spadku predykcji oraz występują podobne dwa spadki jak u obserwacji 57.
Dla zmiennej Avg open to buy(średni limit kredytu odnawialnego z ostatnich 12 miesięcy) obserwacja 57. ma niewielkie zmiany, a dla obserwacji 61. pojawia się duży spadek predykcji dla wartości 1380-3100.
W poprzedniej pracy domowej zastanawiało mnie dlaczego w zależnosci o ile wzrosło Total trans amt czasem dostawaliśmy przeciwnę wpływy na predykcję. Użycie profili Ceteris Paribus pozwala zobaczyć i zrozumieć skąd wynikają te różnice. Profil Ceteris Paribus pokazuje,które zmienne mają kluczowy wpływ na zmianę predykcji dla danej obserwacji i przy jakich wartosciach zmiany te są największe.